<?php defined('SYSPATH') or die('No direct script access.');

class Controller_People extends Controller_Page_Template {

	public function action_index()
	{
		$functions = array(
			array('controller' => '/people', 'name' => 'Overview'),
			array('controller' => '/people/add_user', 'name' => 'Add user'),
		); 
		$this->add_sidebar($functions);
	}
	public function action_overview($project_id)
	{
		$functions = Helper_MyConst::MANAGER_PEOPLE_PRIVILEDGE($project_id);
		$this->add_sidebar($functions);
		
		$this->project = Model::factory('project')->get_project_info($project_id);
		
		$users = Model::factory('user')->get_project_users($project_id,1);
		$this->primary_content = View::factory('/user/overview',array(
			'users' => $users,
		));
	}
	public function action_add_user($project_id)
	{
		$functions = Helper_MyConst::MANAGER_PEOPLE_PRIVILEDGE($project_id);
		$this->add_sidebar($functions);
		
		$project = Model::factory('project')->get_project_info($project_id);
		$this->project = $project;
		$users = Model::factory('user')->get_project_users($project_id,0);
		$this->primary_content = View::factory('/user/add_user',array(
				'users' => $users,
				'project' => $project
		));
	}
	public function action_submit_add_user($project_id)
	{
		$users = explode(',', $_POST['people']);
		$m = MODEL::factory('project');
		foreach ($users as $user)
			$m->add_user_2_project(Helper_MyConst::PROJECT_EMPLOYEE,$user['id'], $project_id);
		$this->request->redirect('/user/overview/'.$project_id);
	}
	public function action_remove_user($project_id)
	{
		$functions = Helper_MyConst::MANAGER_PEOPLE_PRIVILEDGE($project_id);
		$this->add_sidebar($functions);
		
		$project = Model::factory('project')->get_project_info($project_id);
		$this->project = $project;
		$users = Model::factory('user')->get_project_users($project_id,1);
		$this->primary_content = View::factory('main/people/remove_user',array(
				'users' => $users, 
				'project' => $project)
		);
	}
	public function action_submit_remove_user($project_id)
	{
		$users = explode(',', $_POST['people']);
		$m = MODEL::factory('project');
		foreach ($users as $user)
			$m->remove_user_from_project($user['id'], $project_id);
		$this->request->redirect('/user/overview/'.$project_id);
	}
} 